<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Authentication\UserAuthentication;

/*
 *  案件表接口
 *  @author 小林《1814992057@qq.com》
 *  @datetime   2019/8/23
*/

class  LawyerController extends Controller
{

    use UserAuthentication;

    /*
     * 添加律师
     * @return json
     * */
    public function add_lawyer(Request $request){


        $data=$request->only('name','loid');

        if(!preg_match('/^[0-9]{1,10}$/',$data['loid']))  return ThrowApiExc('20003');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');

        $se = DB::table('C_lawyer')
            ->where([
                "name"=>$data['name'],
                "loid"=>$data["loid"]
            ])
            ->first();
        if($se){
            return [
                'status_code'      => 41825,
                'message'     =>  '姓名重复',
                'data' => ""
            ];
        }

        $res = DB::table('C_lawyer')->insertGetId(
            [
                'name'=>$data['name'],
                'loid'=>$data['loid'],
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }

    /*
     * 修改律师
     * @return json
     * */
    public function update_lawyer(Request $request,$lid){

        $data=$request->only('name','description','loid');

        if(!preg_match('/^[0-9]{1,10}$/',$data['loid']))  return ThrowApiExc('20003');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,400}$/u',$data['description'])) return ThrowApiExc('200032');


        $se = DB::table('C_lawyer')
            ->where([
                "name"=>$data['name'],
                "loid"=>$data["loid"]
            ])
            ->first();
        if($se){
            return [
                'status_code'      => 41825,
                'message'     =>  '姓名重复',
                'data' => ""
            ];
        }


        $res = DB::table('C_lawyer')->where('id',$lid)->update(
            [
                'name'=>$data['name'],
                'loid'=>$data['loid'],
                'description'=>$data['description']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }


    /*
     * 删除律师
     * @return json
     * */
    public function delete_lawyer($lid){


        $res=DB::table('C_lawyer')->where('id',$lid)->delete();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40003');
        }

    }


    /*
     * 查找律师
     * @return json
     * */
    public function select_lawyer($loid){

        $where=[];
        if($loid!=0){
            $where[]=["loid","=",$loid];
        }
        $data=DB::table('C_lawyer')
            ->where($where)
            ->get();

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];

    }

    public function select_lawyer_case(Request $request){
        $data=$request->only('name');
        $data=DB::table('C_lawyerOffice')->where($data)->get();

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];
    }


}
